package org.istt.web;

import org.istt.domain.TimeWeekend;
import org.istt.service.TimeWeekendManager;

import com.opensymphony.xwork2.ActionContext;

/**
 * @author Huy
 *
 */
public class TimeWeekendAction extends BaseAction<TimeWeekendManager, TimeWeekend, Long> {
	private TimeWeekend timeWeekend;
	private Long id;
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 9012754123343759656L;
	
	public void setId(Long id) {
		this.id = id;
	}
	@Override
	public Long getId() {
/*		Long id = NumberUtils.toLong(timeWeekend.getId());
		if (id == 0) return null;
		return id;*/
		return (timeWeekend==null? id : timeWeekend.getId());//id
	}


	@Override
	protected void setSaveMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("timeWeekend.saved"));
	}

	@Override
	protected void setDeleteMessage() {
        ActionContext.getContext().getSession().put("message", 
                getText("timeWeekend.deleted"));
	}

	@Override
	public void validate() {
			if (timeWeekend.getDay_id() != null) {
				switch(timeWeekend.getDay_id())
				{
				  case 1:
					  timeWeekend.setDay_name("Th\u1EE9 hai");
					  break;
				  case 2:
					  timeWeekend.setDay_name("Th\u1EE9 ba");
					  break;
				  case 3:
					  timeWeekend.setDay_name("Th\u1EE9 t\u01B0");
					  break;
				  case 4:
					  timeWeekend.setDay_name("Th\u1EE9 n\u0103m");
					  break;
				  case 5:
					  timeWeekend.setDay_name("Th\u1EE9 s\u00E1u");
					  break;
				  case 6:
					  timeWeekend.setDay_name("Th\u1EE9 b\u1EA9y");
					  break;
				  case 7:  
					  timeWeekend.setDay_name("Ch\u1EE7 nh\u1EADt");
					  break;
				}
				
				if(!mgr.isUnique(new String[]{"day_name"}, new String[]{timeWeekend.getDay_name().toString()}, timeWeekend.getId())){
					addActionError(getText("timeWeekend.error.dayName.not.unique"));
				}
			}
		
	}
	@Override
	public String save() {
		if (log.isDebugEnabled()) {
            log.debug("entering 'save' method");
        }

        //entity = getEntity();
        ((TimeWeekendManager)mgr).save(timeWeekend);
        setSaveMessage();
                
        return SUCCESS;
	}

    
	public void setTimeWeekend(TimeWeekend timeWeekend) {
		this.timeWeekend = timeWeekend;
	}

	public TimeWeekend getTimeWeekend() {
		if (entity!= null && entity.getId() != null) {
			return entity;
		}		
		return timeWeekend;
	}

	public boolean isValid() {
		return mgr.isUnique(new String[]{"day_id"}, new String[]{timeWeekend.getDay_id().toString()}, timeWeekend.getId());
	}	
}
